package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.ProductConditionDTO;
import com.tulingxueyuan.mall.dto.ProductPramaterDTO;
import com.tulingxueyuan.mall.dto.ProductUpdateInitDTO;
import com.tulingxueyuan.mall.modules.pms.model.PmsProduct;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author XuShu
 * @since 2023-05-08
 */
@RestController
@RequestMapping("/product")
public class PmsProductController {
    @Autowired
    private PmsProductService pmsProductService;

    @ApiOperation("商品列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage> list(ProductConditionDTO productConditionDTO){
       Page page = pmsProductService.listPage(productConditionDTO);
        CommonPage commonPage = CommonPage.restPage(page);
        return CommonResult.success(commonPage);
    }

    @ApiOperation("删除商品列表")
    @PostMapping(value = "/update/deleteStatus")
    public CommonResult delete(@RequestParam(value = "ids")List<Integer> ids){
        boolean b = pmsProductService.removeByIds(ids);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }

    }

    @ApiOperation("添加商品列表以及管理的属性")
    @PostMapping(value = "/update/{id}")
    public CommonResult update(@PathVariable Long id,@RequestBody @Valid ProductPramaterDTO productPramaterDTO){
        boolean b = pmsProductService.updateWithAll(productPramaterDTO);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }

    }

    @ApiOperation("添加商品列表以及管理的属性")
    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody ProductPramaterDTO productPramaterDTO){
        boolean b = pmsProductService.create(productPramaterDTO);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }

    }




    @ApiOperation("是否上架")
    @PostMapping(value = "/update/publishStatus")
    public CommonResult publishStatus(
            @RequestParam(value = "ids") List<Integer> ids,
            @RequestParam(value = "publishStatus") Integer publishStatus
    ){
        boolean b = pmsProductService.updateStatus(ids,publishStatus,PmsProduct::getPublishStatus);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("是否新品")
    @PostMapping(value = "/update/newStatus")
    public CommonResult newStatus(
            @RequestParam(value = "ids") List<Integer> ids,
            @RequestParam(value = "newStatus") Integer newStatus
    ){
        boolean b = pmsProductService.updateStatus(ids,newStatus,PmsProduct::getNewStatus);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("是否推荐")
    @PostMapping(value = "/update/recommendStatus")
    public CommonResult recommendStatus(
            @RequestParam(value = "ids") List<Integer> ids,
            @RequestParam(value = "recommendStatus") Integer recommendStatus
    ){
        boolean b = pmsProductService.updateStatus(ids,recommendStatus,PmsProduct::getRecommandStatus);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改回显")
    @GetMapping(value = "/updateInfo/{id}")
    public CommonResult<ProductUpdateInitDTO> updateInfo(@PathVariable Long id){
        ProductUpdateInitDTO productUpdateInitDTO = pmsProductService.getUpdateInfo(id);
        return CommonResult.success(productUpdateInitDTO);
    }
}

